package com.invest.infra.excel.export;

import com.invest.infra.template.ExportResponseUtils;
import com.invest.utils.io.FileIOUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * STMS 2012
 * 
 * @author Evan
 * @date 2012-09-16
 * 
 */
public class ExcelWriteUtils {
	
	public static <T> void writeExcel(HttpServletResponse response, ExcelProvider<T> provider){
		
		try {
			
			ExportResponseUtils.setResponse4Excel(response,provider.getExcelName());
			writeExcel(response.getOutputStream(),provider);
			
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		
	}
	
	public static <T> void writeExcel(OutputStream os, ExcelProvider<T> provider){

		try {
			provider.writeTemplateHeader(os);
			provider.writeContent(os);
			provider.writeTemplateFooter(os);
		}finally {
			FileIOUtils.close(os);
		}

	}
}
